﻿///----------------------------------------------------------------
/// Copyright (C) 2008-2010 yyssoft
/// 版权所有。
///
/// 文件名：TB_TEAM_FILEBLL.cs
/// 文件功能描述：Tb_Xmk_FileBiz业务逻辑类
///
///
/// 创建标识：黄永生 2014/3/23
///
/// 修改标识：
/// 修改描述：
///
/// 修改标识：
/// 修改描述：
///---------------------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using XMK.Tadong.Model;
using XMK.Tadong.Data;
using XMK.Utils;
using XMK.Framework.Model;
using System.Configuration;
using System.IO;
using XMK.Tadong.Model.Cond;
namespace XMK.Tadong.Biz
{
    public class Tb_Xmk_FileBiz
    {
        #region  私有变量

        private Tb_Xmk_FileData dal = new Tb_Xmk_FileData(Config.TadongDbProvider);

        #endregion  私有变量

        #region  构造函数

        public Tb_Xmk_FileBiz()
        { }

        #endregion  构造函数

        #region  成员方法
        /// <summary>
        /// 上传文档
        /// </summary>
        /// <param name="fileObj"></param>
        /// <returns></returns>
        public bool Upload(TB_XMK_FILEInfo fileObj)
        {
            bool result = false;

            Tb_Xmk_FileData fileData = new Tb_Xmk_FileData(Config.TadongDbProvider);

            //  if (!fileData.IsExist(fileObj))
            {
                result = fileData.InsertNewFile(fileObj);
            }

            return result;
        }

        /// <summary>
        /// 判断同一目录下是否有多个引用
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool HasSavePath(string Save_Path)
        {
            return dal.HasSavePath(Save_Path);
        }


        public bool isMoreThanOne(string savePath, string fileName)
        {

            Tb_Xmk_FileData fileData = new Tb_Xmk_FileData(Config.TadongDbProvider);
            return fileData.IsMoreThanOne(savePath, fileName);


        }

        public bool Delete(decimal fileId)
        {
            return dal.delete(fileId);
        }

        public bool DeleteIN(string fileId)
        {
            return dal.DeleteIN(fileId);
        }

        /// <summary>
        /// 更新文档
        /// </summary>
        /// <param name="fileObj"></param>
        /// <returns></returns>
        public bool Update(TB_XMK_FILEInfo fileObj)
        {
            bool result = true;
            Tb_Xmk_FileData fileData = new Tb_Xmk_FileData(Config.TadongDbProvider);
            result = fileData.Update(fileObj);
            return result;
        }

        /// <summary>
        /// 重新提交
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool NewAgainDataSubmit(string File_ID)
        {
            return dal.NewAgainDataSubmitBySql(File_ID);
        }

        /// <summary>
        /// 批量审核项目库文件
        /// </summary>
        /// <param name="fileObj"></param>
        /// <returns></returns>
        public bool UpdateDatas(string File_ID, string Folder_ID)
        {
            Tb_Xmk_FileData fileData = new Tb_Xmk_FileData(Config.TadongDbProvider);
            return fileData.UpdateDatasBySQL(File_ID, Folder_ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回主标识ID,失败返回"0"</returns>
        public bool Add(TB_XMK_FILEInfo model)
        {
            try
            {
                return dal.AddBySql(model);
            }
            catch (Exception e)
            {
                return false;
            }
            
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModel(string FileID)
        {
            return dal.GetModelBySql(FileID);
        }

        
        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetxmkModel(string FileID)
        {
            return dal.GetxmkModelBySql(FileID);
        }

        public XMKFileExtInfo GetXMKFileExtInfoByID(string FileID)
        {
            return dal.GetXMKFileExtInfoByID(FileID);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObjAndType(string op, string folder_id, int startNum, int endNum, ref int total)
        {
            return dal.GetModelByDestObjAndType(op, folder_id, startNum, endNum, ref total);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObj(string DestObjId, int startNum, int endNum, ref int total)
        {
            return dal.GetModelByDestObj(DestObjId, startNum, endNum, ref total);

        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObjAndKeyWord(string folder_id, string keyword, int startNum, int endNum, ref int total)
        {
            return dal.GetModelByDestObjAndKeyWord(folder_id, keyword, startNum, endNum, ref total);
        }


        public int  GetCountByDestObj(string DestObjId)
        {
            return dal.GetCountByDestObj(DestObjId);
            //return dal.GetModelByDestObj(DestObjId, startNum, endNum, ref total);

        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public int GetCountByDestObjAndKeyWord(string folder_id, string keyword)
        {
            return dal.GetCountByDestObjAndKeyWord(folder_id, keyword);
        }



        /// <summary>
        /// 获取未审核文件
        /// </summary>
        /// <returns>文件列表</returns>
        public DataTable GetUnauditedModelBySql(string Team_ID, string sorttype, string sortdesc, string status, int startNum, int endNum, ref int total)
        {
            return dal.GetUnauditedModelBySql(Team_ID,sorttype,sortdesc, status, startNum, endNum, ref total);
        }

        /// <summary>
        /// 获取未审核文件 By CaoBin 20140504
        /// </summary>
        /// <returns>文件列表</returns>
        public DataTable GetXMKUnauditedModel(int startNum, string sorttype, string sortdesc, int endNum, ref int total)
        {
            return dal.GetXMKUnauditedModelBySql(startNum, sorttype, sortdesc,endNum, ref total);
        }

        /// <summary>
        /// 获取已审核文件 By CaoBin 20140504
        /// </summary>
        /// <returns>文件列表</returns>
        public DataTable GetXMKAuditModel(int startNum, string sorttype, string sortdesc, int endNum, ref int total)
        {
            return dal.GetXMKAuditModelBySql(startNum, sorttype, sortdesc,endNum, ref total);
        }

        /// <summary>
        /// 退回数据 By CaoBin 20140504
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool BackFile(string SINGLERECEIPT, string FILE_ID)
        {
            return dal.BackFileBySql(SINGLERECEIPT, FILE_ID);
        }

        /// <summary>
        /// 审核数据
        /// </summary>
        /// <param name="FileID">文件编号</param>
        /// <returns>成功返回true，失败返回false</returns>
        public bool AuditData(string FileID)
        {
            return dal.AuditDataBySql(FileID);
        }
        /// <summary>
        /// 根据文件名称、文件路径
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkFileyFileNameAndPath(string FileName, string Path)
        {
            return dal.getXmkFileyFileNameAndPath(FileName, Path);
        }

        /// <summary>
        /// 根据文件名称、文件路径
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getTeamFileyFileNameAndPath(string FileName, string Path)
        {
            return dal.getTeamFileyFileNameAndPath(FileName, Path);
        }

        public TB_XMK_FILEInfo Query(string file_name, string save_path)
        {
            DataTable dt = dal.getTeamFileyFileNameAndPath(file_name, save_path);
            if (dt.Rows.Count > 0)
            {
                TB_XMK_FILEInfo fileInifo = new TB_XMK_FILEInfo();
                DataRow dr = dt.Rows[0];

                fileInifo.FILE_ID = ParseHelper.Parse<decimal>(dr["FILE_ID"]);
                fileInifo.TEAM_ID = null;// ParseHelper.Parse<decimal>(dr["TEAM_ID"]);
                fileInifo.FILE_NAME = Convert.ToString(dr["FILE_NAME"]);
                fileInifo.FILE_EXT = ParseHelper.Parse<string>(dr["FILE_EXT"]);
                fileInifo.FILE_ABST = Convert.ToString(dr["FILE_ABST"]);
                fileInifo.FILE_AUTHOR = Convert.ToString(dr["FILE_AUTHOR"]);
                fileInifo.KEYW = Convert.ToString(dr["KEYW"]);
                fileInifo.FILE_CRT_DATE = ParseHelper.Parse<DateTime>(dr["FILE_CRT_DATE"]);
                fileInifo.FILE_VERSION = Convert.ToDecimal(dr["FILE_VERSION"]);
                fileInifo.DEST_OBJ = ParseHelper.Parse<string>(dr["DEST_OBJ"]);
                fileInifo.BIZ_OBJ = ParseHelper.Parse<string>(dr["BIZ_OBJ"]);
                fileInifo.SECRET_LVL = Convert.ToString(dr["SECRET_LVL"]);
                fileInifo.LANG_TYPE = Convert.ToDecimal(dr["LANG_TYPE"]);
                fileInifo.FILE_TYPE = Convert.ToString(dr["FILE_TYPE"]);
                fileInifo.SAVE_PATH = Convert.ToString(dr["SAVE_PATH"]);
                fileInifo.USER_ID = Convert.ToString(dr["USER_ID"]);
                fileInifo.STATUS = ParseHelper.Parse<decimal>(dr["STATUS"]);
                fileInifo.CREATE_TIME = ParseHelper.Parse<DateTime>(dr["CREATE_TIME"]);
                fileInifo.UPD_TIME = ParseHelper.Parse<DateTime>(dr["UPD_TIME"]);
                fileInifo.SINGLERECEIPT = Convert.ToString(dr["SINGLERECEIPT"]);
                fileInifo.PDF_PATH = Convert.ToString(dr["PDF_PATH"]);
                fileInifo.SWF_PATH = Convert.ToString(dr["SWF_PATH"]);

                return fileInifo;

            }
            return null;

        }



        public TB_XMK_FILEInfo QueryById(string fileId)
        {
            DataTable dt = dal.QueryById(fileId);
            if (dt.Rows.Count > 0)
            {
                TB_XMK_FILEInfo fileInifo = new TB_XMK_FILEInfo();
                DataRow dr = dt.Rows[0];

                fileInifo.FILE_ID = ParseHelper.Parse<decimal>(dr["FILE_ID"]);
                if (Convert.IsDBNull(dr["TEAM_ID"]))
                {
                    fileInifo.TEAM_ID = null;
                }
                else
                {
                    fileInifo.TEAM_ID = ParseHelper.Parse<decimal>(dr["TEAM_ID"]);
                }

                fileInifo.FILE_NAME = Convert.ToString(dr["FILE_NAME"]);
                fileInifo.FILE_EXT = Convert.ToString(dr["FILE_EXT"]);
                fileInifo.FILE_ABST = Convert.ToString(dr["FILE_ABST"]);
                fileInifo.FILE_AUTHOR = Convert.ToString(dr["FILE_AUTHOR"]);
                fileInifo.KEYW = Convert.ToString(dr["KEYW"]);

                if (!Convert.IsDBNull(dr["FILE_CRT_DATE"]))
                {
                    fileInifo.FILE_CRT_DATE = Convert.ToDateTime(dr["FILE_CRT_DATE"]);
                }

                fileInifo.FILE_CRT_DATE = ParseHelper.Parse<DateTime>(dr["FILE_CRT_DATE"]);
                fileInifo.FILE_VERSION = Convert.ToDecimal(dr["FILE_VERSION"]);
                fileInifo.DEST_OBJ = ParseHelper.Parse<string>(dr["DEST_OBJ"]);
                fileInifo.BIZ_OBJ = ParseHelper.Parse<string>(dr["BIZ_OBJ"]);
                fileInifo.SECRET_LVL = Convert.ToString(dr["SECRET_LVL"]);
                fileInifo.LANG_TYPE = Convert.ToDecimal(dr["LANG_TYPE"]);
                fileInifo.FILE_TYPE = Convert.ToString(dr["FILE_TYPE"]);
                fileInifo.SAVE_PATH = Convert.ToString(dr["SAVE_PATH"]);
                fileInifo.USER_ID = Convert.ToString(dr["USER_ID"]);
                fileInifo.STATUS = ParseHelper.Parse<decimal>(dr["STATUS"]);
                fileInifo.CREATE_TIME = ParseHelper.Parse<DateTime>(dr["CREATE_TIME"]);
                fileInifo.UPD_TIME = ParseHelper.Parse<DateTime>(dr["UPD_TIME"]);
                fileInifo.SINGLERECEIPT = Convert.ToString(dr["SINGLERECEIPT"]);
                fileInifo.PDF_PATH = Convert.ToString(dr["PDF_PATH"]);
                fileInifo.SWF_PATH = Convert.ToString(dr["SWF_PATH"]);
                fileInifo.FOLDER_ID = ParseHelper.Parse<decimal>(dr["FOLDER_ID"]);

                return fileInifo;

            }
            return null;

        }
        /// <summary>
        /// 上传不可转换文件预览图片
        /// </summary>
        /// <param name="FileID"></param>
        /// <returns></returns>
        public bool PrePicAdd(PrePicCond cond )
        {
            return dal.PrePicAdd(cond);
        }

        /// <summary>
        /// 修改源数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateSourceData(TB_XMK_FILEInfo model)
        {
            return dal.UpDateSourceDataBySql(model);
        }

        /// <summary>
        /// 重新提交
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool AgainDataSubmit(TB_XMK_FILEInfo model, string Flag)
        {
            return dal.AgainDataSubmitBySql(model, "1");
        }

        /// <summary>
        /// 修改关键词和摘要信息
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateInfo(TB_XMK_FILEInfo model, string types)
        {
            return dal.UpDateInfoBySql(model, types);
        }

        //根据文件ID将文件移动到回收站
        public bool RemoveToRecycle(string File_ID)
        {
            return dal.RemoveToRecycleBySql(File_ID);
        }

        #region 项目库文件夹管理

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetTree_Xmk(string OBJ_ID)
        {
            return dal.GetTree_XmkBySql(OBJ_ID);
        }

        /// <summary>
        /// 退回数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateTree_Xmk(TB_Tree_ObjectInfo model)
        {
            return dal.UpdateTree_XmkBySql(model);
        }

        /// <summary>
        /// 项目库文件夹剪切
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateTree_XmkCUT(TB_Tree_ObjectInfo model)
        {
            return dal.UpdateTree_XmkCUTBySql(model);
        }

        /// <summary>
        /// 根据文件名称、文件路径
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkFileByFOLDER_ID(string FOLDER_ID)
        {
            return dal.getXmkFileByFOLDER_ID(FOLDER_ID);
        }


         /// <summary>
        /// 获取子文件夹文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetSTree_XmkBySql(string OBJ_ID)
        {
            return dal.GetSTree_XmkBySql(OBJ_ID);
        }

         //删除文件夹
        public bool deleteTREE_XMK(string OBJ_ID)
        {
            return dal.deleteTREE_XMK(OBJ_ID);
        }

        #endregion 项目库文件夹管理


        /// <summary>
        /// 获取回收站信息
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>结果集</returns>
        public DataTable GetRecycleDataBySql(int startNum, string sorttype, string sortdesc, int endNum, ref int total)
        {
            return dal.GetRecycleDataBySql(startNum, sorttype, sortdesc,endNum, ref total);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件IDs</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelList(string FileID)
        {
            return dal.GetModelListBySql(FileID);
        }

        //根据文件ID将文件从回收站移回空间
        public bool RecycleToSpace(string File_ID)
        {
            return dal.RecycleToSpaceBySql(File_ID);
        }
        //获取项目库空间同名文件最大版本号
        public decimal GetMaxFileVersionXmk(string fileName)
        {
            return dal.GetMaxFileVersionXmk(fileName);

        }
        /// <summary>
        /// 获取工区文件列表
        /// </summary>
        /// <param name="gqName">工区名称</param>
        /// <returns>获取工区文件列表</returns>
        public DataTable GetGQFiles(string gqName)
        {
            return dal.GetGQFiles(gqName);
        }
        /// <summary>
        /// 压缩下载文件
        /// </summary>
        /// <param name="fileids"></param>
        /// <returns></returns>
        public ResponseInfo ZipFile(string paths)
        {
            ResponseInfo response = null;
            response = new ResponseInfo();
            
            string sourcepath = "D:/TaDongDoc/";///ConfigurationManager.AppSettings["DiskPath"];
            string strTmpDir = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Temp\\";
            string strTmpDirZip = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "TempZip\\";
            string[] pathss = paths.Split(',');
            if (pathss.Length <= 1)
            {
                 Directory.CreateDirectory(strTmpDir);
                 Directory.Delete(strTmpDir, true);
                 Directory.CreateDirectory(strTmpDir);
                 //如果只是一个文件的话，直接下载咯\
                 Tb_Xmk_FileBiz bll = new Tb_Xmk_FileBiz();
                 DataTable dt = bll.GetModelList(paths);
                 string sourcdownepath = "";
                 if (dt.Rows.Count > 0)
                 {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sourcdownepath += sourcepath + dt.Rows[i]["save_path"].ToString();
                    }
                 }
                 File.Copy(sourcdownepath, strTmpDir + dt.Rows[0]["file_name"].ToString());
                 response.Data = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port + System.Web.HttpContext.Current.Request.ApplicationPath + "/Temp/" + dt.Rows[0]["file_name"].ToString();

            }
            return response;
            
            
        }

        /// <summary>
        /// 获取项目库空间所有文件
        /// </summary>
        /// <returns></returns>
        public DataTable getXmkFileIdAndObj()
        {
            return dal.getXmkFileIdAndObj();
        }

        /// <summary>
        /// 分类查看查询数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassFlie(string FOLDER_ID,string classType)
        {
            return dal.getXmkClassFlie(FOLDER_ID, classType);
        }
        /// <summary>
        /// 分类查看明细数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassDetailFile(string FOLDER_ID, string sqlwhere)
        {
            return dal.getXmkClassDetailFile(FOLDER_ID, sqlwhere);
        }
        /// <summary>
        /// 获?取¨?录?井?文?件t列¢D表À¨ª
        /// </summary>
        /// <param name="wellName">井?名?称?</param>
        /// <returns>获?取¨?工¡è区?文?件t列¢D表À¨ª</returns>
        public DataTable GetLoggingFiles(string wellName)
        {
            return dal.GetLoggingFiles(wellName);
        }

        #endregion  成员方法
    }
}
